package com.bc.web.controller.admin;

import com.bc.common.CommonEnum.Resp;
import com.bc.common.message.BaseResp;
import com.bc.web.model.Channel;
import com.bc.web.model.ClearingIncome;
import com.bc.web.service.ClearingIncomeService;
import com.fs.common.extend.FSController;
import com.fs.common.extend.FSSearchCondition;
import com.fs.common.util.FSDate;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

public class ClearingIncomeController extends FSController {
	public void search() {
		FSSearchCondition searchCondition = super.getSearchCondition();
		searchCondition.setParameter("type", "0");
		Page<ClearingIncome> items = ClearingIncome.dao.search(searchCondition);
		setAttr("channelList", Channel.dao.selectAll());
		setPageResultToRequest("items", items);
		render("clearing/income/incomeList.html");
	}
	
	/**
	 * 渠道收入
	 */
	public void getChannelIncome() {
		String settleDate = getPara("settleDateStart");
		if (StrKit.isBlank(settleDate)) {
			renderFail("起始日切日期不能为空", false);
			return;
		}
		if (settleDate.compareTo(FSDate.getDate()) >= 0 || settleDate.compareTo("2018-03-22") < 0) {
			renderFail("提取失败，起始日切日期要小于当天,不能小于2018-03-22", false);
			return;
		}
		ClearingIncomeService clearingIncomeService = new ClearingIncomeService();
		BaseResp resp = clearingIncomeService.channelIncome(settleDate);
		if (Resp.Success.getCode().equals(resp.getRespCode())) {
			renderSuccess("提取渠道收入成功", false);
		} else {
			renderFail("提取渠道收入失败", false);
		}
	}
	
	/**
	 * 分润页面
	 */
	public void shareSearch(){
		FSSearchCondition searchCondition = super.getSearchCondition();
		searchCondition.setParameter("type", "1");
		Page<ClearingIncome> items = ClearingIncome.dao.search(searchCondition);
		setPageResultToRequest("items", items);
		render("clearing/income/shareList.html");
	}
	
	/**
	 * 机构分润
	 */
	public void share(){
		String settleDate = getPara("settleDateStart");
		if (StrKit.isBlank(settleDate)) {
			renderFail("起始日切日期不能为空", false);
			return;
		}
		if (settleDate.compareTo(FSDate.getDate()) >= 0 || settleDate.compareTo("2018-03-22") < 0) {
			renderFail("提取失败，起始日切日期要小于当天,不能小于2018-03-22", false);
			return;
		}
		ClearingIncomeService clearingIncomeService = new ClearingIncomeService();
		BaseResp resp = clearingIncomeService.orgShare(settleDate);
		if (Resp.Success.getCode().equals(resp.getRespCode())) {
			renderSuccess("分润成功", false);
		} else {
			renderFail("分润失败", false);
		}
	}
}
